package kom;

import java.util.UUID;
import kom.Effects.AnimatedEffect;
import kom.Effects.EffectType;
import kom.item.ToolItem;
import kom.item.ToolType;

public class Player extends LivingEntity {

    private UUID id = UUID.randomUUID();
    private boolean animForward = true;
    private int slipSmokeTickCount = 5;
    private int slipSmokeCount = 0;
    private Weapon weapon = null;
    private int currentIndex = 0;
    private int[] walkFrames = {0, 1, 2};
    private Inventory inventory = new Inventory();

    public Player() {
        super(2, 0);
        width = 12;
        height = 14;

        inventory.add(0, new ToolItem(ToolType.pickaxe, 0));
    }

    @Override
    public void tick() {
        animationTick();
        tickAnim++;
    }

//////Replaced by new item system
//    public void equipWeapon(Weapon w) {
//        weapon = w;
//        int[] frameset = {6, 7, 8};
//        walkFrames = frameset;
//    }
//    public void unequipWeapon(Weapon w) {
//        weapon = null;
//        int[] frameset = {0, 1, 2};
//        walkFrames = frameset;
//    }
    private void animationTick() {

        // it wont change its animation state if its stunned
        if (stunned) {
            return;
        }
        if (jumping || aceleration.y < 0) {
            if (weapon != null) {
                frame = 8;
            } else {
                frame = 9;
            }
            return;
        } else if (applyGravity && aceleration.y > 0) {
            if (weapon != null) {
                frame = 6;
            } else {
                frame = 10;
            }
            return;
        }
        // if its iddle, iddle frame
        if (aceleration.getX() == 0 && aceleration.getY() == 0) {
            frame = walkFrames[1];
        } else {

            // makes it animates faster when he moves faster
            // gotta improve this later to dont use fixed numbers
            if (aceleration.x >= maxSpeed || aceleration.x <= -maxSpeed) {
                ticksToAnimate = 3;
            } else if (aceleration.x >= maxSpeed / 3 || aceleration.x <= -maxSpeed / 3) {
                ticksToAnimate = 4;
            } else {
                ticksToAnimate = 6;
            }
            // maaking the "slip" when you turn while moving
            if (aceleration.x > 1 && direction == Map.DIR_LEFT && weapon == null || (weapon != null && weapon.allowSlide)) {
                slipSmokeCount++;
                if (slipSmokeCount >= slipSmokeTickCount) {
                    AnimatedEffect eff = Map.mapEffects.getNewEffect((int) getFutureBox().x, (int) y + 10, EffectType.SMOKE, 0, 0);
                    eff.maxFrame = 6;
                    eff.ticksToFrame = 1;
                    Map.mapEffects.playEffect(eff);
                    slipSmokeCount = 0;
                } else {
                    slipSmokeCount++;
                }
                frame = 3;
            } else if (aceleration.x < -1 && direction == Map.DIR_RIGHT && weapon == null || (weapon != null && weapon.allowSlide)) {
                if (slipSmokeCount >= slipSmokeTickCount) {
                    AnimatedEffect eff = Map.mapEffects.getNewEffect((int) getFutureBox().x, (int) y + 10, EffectType.SMOKE, 0, 0);
                    eff.maxFrame = 6;
                    eff.ticksToFrame = 1;
                    Map.mapEffects.playEffect(eff);
                    slipSmokeCount = 0;
                } else ////////////////////////////////
                // THIS IS A HUGE BUG UNKNOWN //
                // WHY IS THIS LESS CALLED THEN THE RIGHT DIRECTION ?????
                // DAMN !
                {
                    slipSmokeCount += 3;
                }
                frame = 3;
            } else {
                if (tickAnim >= ticksToAnimate) {
                    tickAnim = 0;
                    if (currentIndex == walkFrames.length - 1) {
                        animForward = false;
                    } else if (currentIndex == 0) {
                        animForward = true;
                    }
                    if (animForward) {
                        currentIndex++;
                    } else {
                        currentIndex--;
                    }
                    if (currentIndex < walkFrames.length) {
                        frame = walkFrames[currentIndex];
                    }
                }
            }
        }
    }

    @Override
    public void setFacing(int f) {
        if (weapon == null) {
            facing = f;
            return;
        }
        if (f == Map.DIR_RIGHT && aceleration.x > 0) {
            this.facing = f;
        }
        if (f == Map.DIR_LEFT && aceleration.x < 0) {
            this.facing = f;
        }
    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }
}
